package com.mvc.framework.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

import org.hibernate.annotations.AccessType;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

/**
 * Unique primary key
 * 
 * @author snowpbx
 */
@MappedSuperclass
@Cache(usage = CacheConcurrencyStrategy.NONE)
@AccessType("field")
public class BaseEntity implements Identifiable {
	/**
	 * Primary key
	 * Generated by table <code>id_sequences</code>
	 */
	@GenericGenerator(
			strategy="org.hibernate.id.MultipleHiLoPerTableGenerator",
			name="hiloTable",
			parameters = { @Parameter(name = "max_lo", value = "50"),@Parameter(name = "table", value = "id_sequences")} )
	@Id
	@GeneratedValue(strategy=GenerationType.TABLE, generator="hiloTable")
	private Integer id;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String toString() {
		return String.valueOf(id);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}

		if (getClass() != obj.getClass()) {
			return false;
		}
		BaseEntity other = (BaseEntity) obj;
		if (id == null) {
			if (other.id != null) {
				return false;
			}

		} else if (!id.equals(other.id)) {
			return false;
		}
		return true;
	}
	
	public String getTableName(){
		return getTableName(this.getClass());
	}

	public static String getTableName(Class clazz) {
		Entity table = (Entity) clazz.getAnnotation(Entity.class);
		String tableName = table.name();
		return tableName;
	}
}
